package com.cn.qgx428.lazydiary.ui.activity.subActivity

import android.content.Context
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.cn.qgx428.lazydiary.Presenter.PlanPresenter
import com.cn.qgx428.lazydiary.Presenter.contract.PlanContract
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.MConst
import com.cn.qgx428.lazydiary.adapter.RAdapter
import com.cn.qgx428.lazydiary.adapter.RViewHolder
import com.cn.qgx428.lazydiary.dialog.CustomAlertDialog
import com.cn.qgx428.lazydiary.extension.getCurrenTimePrecise
import com.cn.qgx428.lazydiary.extension.switchVisible
import com.cn.qgx428.lazydiary.network.Imageloader
import com.cn.qgx428.lazydiary.network.bean.PlanDetail
import com.cn.qgx428.lazydiary.ui.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_plan_modify.*
import okhttp3.internal.Util
import org.jetbrains.anko.toast
import org.w3c.dom.Text
import java.util.*

class PlanModifyActivity : BaseActivity(), PlanContract.IPlanDetail {

    var planId: Int = -1
    var planImage = ""
    val adapter: PDetailAdapter = PDetailAdapter(this,R.layout.adapter_plan_modify_item)
    var planDetails: MutableList<PlanDetail> = mutableListOf()

    override fun initUI() {
        hindActionBar()
        setStatuBarColor(Color.BLACK)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_plan_modify)
//
        planId = intent.extras.getInt(MConst.PLAN_ID,-1)
        planImage = intent.extras.getString(MConst.PlAN_IMAGE,"")
        initView()
        getPlanData()
    }

    private fun initView() {
        Imageloader.download(this, planModify_bg,planImage)
        adapter.setDel { delPlanDetail(it) }
        planModify_re.adapter = adapter
        planModify_re.layoutManager = LinearLayoutManager(this)
        planModify_back.setOnClickListener({ finish() })
        planModify_add.setOnClickListener({
            getInputDialog({
                createNewPlanContent(it)
            }).show()
        })
        planModify_commit.setOnClickListener({
            updatePlanDetail()
        })
    }

    /**
     * 更新计划内容
     */
    private fun updatePlanDetail(){
        PlanPresenter.updatePlanDetail(planDetails,{
            toast("更新成功")
            finish()
        })
    }

    /**
     * 删除计划内容
     */
    private fun delPlanDetail(position: Int){
        // 删除分两种情况，加入数据库的和没有加入数据库的
        if (planDetails[position].id == -1){
            planDetails.removeAt(position)
            adapter.setDatas(planDetails)
        } else{
            PlanPresenter.removePlanDetail(position,{
                toast(it)
                planDetails.removeAt(position)
                adapter.setDatas(planDetails)
            })
        }
    }

    private fun createNewPlanContent(content: String) {
        if (planModify_prump.visibility == View.VISIBLE)
            planModify_prump.switchVisible()
        val pd = PlanDetail(planId, content, Calendar.getInstance().getCurrenTimePrecise())
        planDetails.add(pd)
        adapter.setDatas(planDetails)
    }

    private fun getPlanData() {
        PlanPresenter.getPlanDetail(planId,this)
    }


    /**
     * planDetail 获取监听
     */
    override fun onGetPlanDetalData(details: List<PlanDetail>) {
        planModify_prump.switchVisible()
        planDetails = details as MutableList<PlanDetail>
        adapter.setDatas(planDetails)
    }

    override fun onNoPlanDetailData() {

    }

    class PDetailAdapter: RAdapter<PlanDetail>{

        var delListener: ((position: Int) -> Unit)? = null

        constructor(context: Context, layoutId: Int):super(context, layoutId)

        override fun conver(holder: RViewHolder, data: PlanDetail) {
            val name = holder.getView<TextView>(R.id.adapter_planModify_name)
            val del = holder.getView<ImageView>(R.id.adapter_planModify_del)
            name?.text = data.content
            del?.setOnClickListener({
                for (i in 0..getDatas().size-1){
                    if (getDatas()[i].createtime.equals(data.createtime)){
                        if (delListener!=null){
                            delListener?.invoke(i)
                            break
                        }
                    }
                }
            })
        }

        public fun setDel(del: (position: Int)->Unit){
            delListener = del
        }
    }


}
